(function () {
    Array.prototype.contains = function (needle) {
        for (var i in this) {
            if (this[i] === needle) {
                return true;
            }
        }
        return false;
    };

    angular.module('calculate', []);

    angular.module('calculate').service('calculateService', calculateService);

    angular.module('calculate').controller('HistoryLogController', HistoryLogController);

    angular.module('calculate').controller('CurrLogController', CurrLogController);

    angular.module('calculate').controller('CalculateBtnController', CalculateBtnController);
})();

function HistoryLogController($rootScope, calculateService) {
    $rootScope.historyData = calculateService.getHistoryData();
    this.clearHistoryLog = function () {
        calculateService.setHistoryList([]);
        $rootScope.historyData = calculateService.getHistoryData();
    }
}

function CurrLogController($rootScope, calculateService) {
    $rootScope.currentData = calculateService.getCurrentData();
}

function CalculateBtnController($rootScope, calculateService) {
    var vm = this;
    vm.btnData = calculateService.getBtnData();
    vm.clickTd = function (name) {
        if (calculateService.add(name)) {
            $rootScope.currentData = calculateService.getCurrentData();
        }
        if ('=' === name) {
            $rootScope.historyData = calculateService.getHistoryData();
        }
    }
}

function calculateService() {
    this.historyList = [];
    this.currentList = [];
    this.currentResult = '0';
    this.add = add;
    this.getBtnData = getBtnData;
    this.getCurrentData = getCurrentData;
    this.getHistoryData = getHistoryData;
    this.setHistoryList = setHistoryList;

    var afterClickEqual = false;//按完等号后
    var num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'];
    var symbol = ['＋', '－', '×', '÷'];
    var service = this;

    function add(name) {//添加一位数字
        if (afterClickEqual) {//按完等号后，再次输入，会清空数据
            service.currentList = [];
            service.currentResult = '0';
            afterClickEqual = false;
        }
        var length = service.currentList.length;
        if (isNum(name)) {
            if (length === 0) {
                service.currentList[0] = name;
                return true;
            }
            var last = service.currentList[length - 1];
            if (isSymbol(last)) {//增加一位
                service.currentList[length] = name;
                return true;
            } else {
                if ('.' === name) {//避免出现两个小数点
                    if (!last.contains('.')) {
                        service.currentList[length - 1] = last + name;
                        return true;
                    }
                    return false;
                }
                if ('0' === last) {//数字0后面只能是小数点
                    return false;
                }
                service.currentList[length - 1] = last + name;
                return true;
            }
        } else if (isSymbol(name)) {
            if (length === 0) {//不能先输入符号
                return false;
            }
            if (!isSymbol(service.currentList[length - 1])) {//最后一位不是符号，才能加
                service.currentList[length] = name;
                service.currentResult = calculateResult();
                return true;
            }
            return false;
        } else if ('=' === name && length > 0) {
            service.currentResult = calculateResult();
            service.historyList[service.historyList.length] = getCurrentData();
            afterClickEqual = true;
            return true;
        } else {
            return false;
        }
    }

    function getBtnData() {
        var btnData = [
            [{'name': 'sin'}, {'name': 'sin'}, {'name': 'sin'}, {'name': 'C'}, {'name': '()'}, {'name': '%'}, {'name': '÷'}],
            [{'name': 'sin'}, {'name': 'sin'}, {'name': 'sin'}, {'name': '7'}, {'name': '8'}, {'name': '9'}, {'name': '×'}],
            [{'name': 'sin'}, {'name': 'sin'}, {'name': 'sin'}, {'name': '4'}, {'name': '5'}, {'name': '6'}, {'name': '－'}],
            [{'name': 'sin'}, {'name': 'sin'}, {'name': 'sin'}, {'name': '1'}, {'name': '2'}, {'name': '3'}, {'name': '＋'}],
            [{'name': 'sin'}, {'name': 'sin'}, {'name': 'sin'}, {'name': '.'}, {'name': '0'}, {'name': '±'}, {'name': '='}]];
        for (var i = 0; i < btnData.length; i++) {
            for (var j = 0; j < btnData[i].length; j++) {
                var obj = btnData[i][j];
                if (j <= 2) {
                    obj.class = 'left';
                } else if (i === 0) {
                    obj.class = 'top';
                } else if (i === 4 && j === 6) {
                    obj.class = 'equal';
                } else if (j === 6) {
                    obj.class = 'top';
                } else {
                    obj.class = 'num';
                }
            }
        }
        return btnData;
    }

    function getCurrentData() {
        var list = [];
        for (var i = 0; i < service.currentList.length; i++) {
            //0表示数字，1表示符号
            list[i] = new Calculate(service.currentList[i], i % 2);
        }
        return {'result': service.currentResult, 'list': list};
    }

    //获取历史数据
    function getHistoryData() {
        return service.historyList;
    }

    function setHistoryList(data) {
        service.historyList = data;
    }

    //计算结果
    function calculateResult() {
        var result = 0.0;
        var currSymbol = null;
        for (var i = 0; i < service.currentList.length; i++) {
            var value = service.currentList[i];
            if (isSymbol(value)) {
                currSymbol = value;
            } else if (i === 0) {
                result = parseFloat(value);
            } else {
                value = parseFloat(value);
                if (currSymbol === symbol[0]) {
                    result += value;
                } else if (currSymbol === symbol[1]) {
                    result -= value;
                } else if (currSymbol === symbol[2]) {
                    result *= value;
                } else if (currSymbol === symbol[3]) {
                    result /= value;
                }
            }
        }
        return result;
    }

    function isNum(name) {
        return num.contains(name);
    }

    function isSymbol(name) {
        return symbol.contains(name);
    }
}

function Calculate(text, type) {
    this.text = text;
    this.type = type;
}

